<?php

require_once ROOT_SERVICES . DS . 'shipping_provider_interface.php';
require_once ROOT_LIBRARY_HTMLDOM . DS . 'simple_html_dom.php';

// viettel_post
class shipping_provider_6 implements shipping_provider_interface {

    private $_shipment;

    public function __construct($shipment) {
        $this->_shipment = $shipment;
    }

    public function get_status_shipping() {
        $tracking_number = $this->_shipment['tracking_number'];
        $str_link = $this->_shipment['url'] !== '' ? $this->_shipment ['url'] : "http://sbplogistics.com:8080/default.aspx?TrackingNumber=";
        $str_link .= $tracking_number;

		$status_list = array();
        try
        {
            $html = file_get_html($str_link);
            $table = $html->find('table.TblGrid', 0);
            if (isset($table)) {
                foreach ($table->find('.tr0,.tr1') as $tr) {
                    $tds = $tr->find('td');
                    if (!empty($tds)) {
                        $status = $tds[1]->plaintext;
                        $time = $tds[2]->plaintext;

                        list($j, $m, $Y, $H, $i, $s) = sscanf($time, '%2d-%2d-%4d %2d:%2d:%2d');

                        $status_list [] = array(
                            'time' => "{$Y}-{$m}-{$j} {$H}:{$i}:{$s}",
                            'status' => "({$this->_shipment ['shipment_provider_name']}) " . $status
                        );
                    }
                }
            }
            return array('sortable' => TRUE, 'status_list' => $status_list);
            
        } catch (Exception $e)
        {
            return array('sortable' => FALSE, 'status_list' => null);
        }
    }

}

?>
